﻿using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
using LuckyFish.FileManager.Models;
using LuckyFish.FileManager.Serves;

namespace LuckyFish.FileManager.Views;

public partial class SettingView : Window
{
    private SettingModel? _model;

    public SettingView()
    {
        InitializeComponent();
        ComboBox = this.FindControl<ComboBox>("ComboBox");
        _model = SettingServer.Read();
        if (_model == null) return;
        ComboBox.ItemsSource = new[] { "Image", "Column" };
        ComboBox.SelectedIndex = _model.FinderTheme == "Image" ? 0 : 1;
    }

    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
    }

    private void ThemeChangeClick(object? sender, RoutedEventArgs e)
    {
        Application.Current!.RequestedThemeVariant = Application.Current.ActualThemeVariant == ThemeVariant.Dark
            ? ThemeVariant.Light
            : ThemeVariant.Dark;
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        var s = new[] { "Image", "Column" }[ComboBox.SelectedIndex];
        if (_model?.FinderTheme == s) return;
        SettingServer.Save(new SettingModel() { FinderTheme = s });
    }

    private void QuitClick(object? sender, RoutedEventArgs e)
    {
        Close();
    }
}